package lumis.portal.principal.serialization;

import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.group.GroupConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.manager.ManagerFactoryInternal;
import lumis.portal.principal.PrincipalConfig;
import lumis.portal.serialization.Dependency;
import lumis.portal.serialization.ISerializationMessageConfigNodeBuilder;
import lumis.portal.serialization.SerializationContext;
import lumis.portal.user.UserConfig;
import lumis.util.ITransaction;

/**
 *Manager for Principal serialization 
 *
 * @version $Revision: 12710 $ $Date: 2011-03-18 17:00:16 -0300 (Fri, 18 Mar 2011) $
 * @since 4.0.10
 */
public class PrincipalSerializationManager implements IPrincipalSerializationManager
{
	public void analyzeExternalDependencies(SerializationContext serializationContext, ISerializationMessageConfigNodeBuilder serializationMessageConfigNodeBuilder, String principalId) throws PortalException
	{
		PrincipalConfig principalConfig = ManagerFactory.getPrincipalManager().get(serializationContext.getSessionConfig(), principalId, serializationContext.getTransaction());

		if (principalConfig.getChannelId() != null && serializationContext.getChannelIds().contains(principalConfig.getChannelId()))
			return;

		UserConfig dependableUserConfig = null;
		GroupConfig dependableGroupConfig = null;

		switch (principalConfig.getType())
		{
			case PrincipalConfig.TYPE_USER:
				dependableUserConfig = ManagerFactory.getUserManager().get(serializationContext.getSessionConfig(), principalConfig.getId(), serializationContext.getTransaction());
				if (dependableUserConfig.getType() != UserConfig.USER_TYPE_COMMON)
					return;
				break;
			case PrincipalConfig.TYPE_GROUP:
				dependableGroupConfig = ManagerFactory.getGroupManager().get(serializationContext.getSessionConfig(), principalConfig.getId(), serializationContext.getTransaction());
				if (dependableGroupConfig.getType() != GroupConfig.GROUP_TYPE_STANDARD)
					return;
				break;
		}

		Dependency dependency = new Dependency();
		dependency.setDependentNode(serializationMessageConfigNodeBuilder.build());
		switch (principalConfig.getType())
		{
			case PrincipalConfig.TYPE_USER:
				dependency.setDependableNode(ManagerFactoryInternal.getUserSerializationManager().getSerializationMessageConfigNode(serializationContext, dependableUserConfig));
				break;
			case PrincipalConfig.TYPE_GROUP:
				dependency.setDependableNode(ManagerFactoryInternal.getGroupSerializationManager().getSerializationMessageConfigNode(serializationContext, dependableGroupConfig));
				break;
		}

		serializationContext.getExternalDependencies().add(dependency);
	}
	
	public boolean isGlobalPrincipal(SessionConfig sessionConfig, String principalId, ITransaction transaction) throws PortalException
	{
		PrincipalConfig principalConfig = ManagerFactory.getPrincipalManager().get(sessionConfig, principalId, transaction);
		return principalConfig.getChannelId() == null && GroupConfig.GROUP_TYPE_STANDARD == principalConfig.getSubType();
		
	}
}
